package com.cmccgz.baselibrary.utils;

import java.util.Random;

/**
 * @author: Huang
 * @Descripion:
 * @date 2017/7/17 09:58
 */

public class ByteHexStringUtils {


    /**
     * 字节转十六进制字符串
     *
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


    /**
     * 十六进制字符串转字节
     *
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    /**
     * 产生十六进制字符串
     *
     * @param len
     * @return
     */
    public static String randomHexString(int len) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < len; i++) {
            builder.append(Integer.toHexString(new Random().nextInt(16)));
        }
        return builder.toString().toUpperCase();
    }

    /**
     * 校验和算法
     *
     * @param msg    数据字节
     * @param length 和长度
     * @return
     */
    public static byte[] SumCheck(byte[] msg, int length) {
        long mSum = 0;
        byte[] mByte = new byte[length];

        // 逐Byte添加位数和
        for (byte byteMsg : msg) {
            long mNum = ((long) byteMsg >= 0) ? (long) byteMsg : ((long) byteMsg + 256);
            mSum += mNum;
        }

        // 位数和转化为Byte数组
        for (int liv_Count = 0; liv_Count < length; liv_Count++) {
            mByte[length - liv_Count - 1] = (byte) (mSum >> (liv_Count * 8) & 0xff);
        }

        return mByte;
    }

    /**
     * 指令转译
     *
     * @param code
     * @return
     */
    public static String translation(String code) {
        String upperCaseCode = code.toUpperCase();
        String replace7FCode = upperCaseCode.replaceAll("7F", "7F02");
        return replace7FCode.replaceAll("7E", "7F01");
    }

    /**
     * 指令反转译
     *
     * @param code
     * @return
     */
    public static String reverseTranslation(String code) {
        String upperCaseCode = code.toUpperCase();
        String replace7F01Code = upperCaseCode.replaceAll("7F01", "7E");
        return replace7F01Code.replaceAll("7F02", "7F");
    }
}
